package com.sword.demo.quartz.interfaces.api;

import com.sword.demo.quartz.service.SchedulerService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.InvocationTargetException;

/**
 * 定时任务-接口
 * @author sword
 * @date 2022/11/29 15:33
 */
@RestController
@RequestMapping("/scheduler-job")
@Tag(name = "SchedulerApi", description = "定时任务-接口")
@RequiredArgsConstructor
public class SchedulerApi {

    /**
     * 定时任务-服务
     */
    private final SchedulerService schedulerService;

    /**
     * 手动执行指定定时任务详情
     * @param jobDetailId 定时任务详情bean的id
     * @author sword
     * @date 2022/11/29 15:33
     */
    @PostMapping("/invokeJobDetail")
    @Operation(summary = "手动执行指定定时任务详情")
    @Parameter(name = "jobDetailId", description = "定时任务详情bean的id", in = ParameterIn.QUERY,
            example = "clockJobDetail")
    public void invokeJobDetail(String jobDetailId) throws InvocationTargetException, IllegalAccessException {
        schedulerService.invokeJobDetail(jobDetailId);
    }

    /**
     * 启动定时任务
     * @author sword
     * @date 2022/11/29 15:33
     */
    @PostMapping("/start")
    @Operation(summary = "启动定时任务")
    public void start() throws SchedulerException {
        schedulerService.start();
    }

    /**
     * 暂停定时任务，可以重新启动
     * @author sword
     * @date 2022/11/29 15:33
     */
    @PostMapping("/standby")
    @Operation(summary = "暂停定时任务，可以重新启动")
    public void standby() throws SchedulerException {
        schedulerService.standby();
    }

    /**
     * 关闭定时任务，无法重新启动
     * @author sword
     * @date 2022/11/29 15:33
     */
    @PostMapping("/shutdown")
    @Operation(summary = "关闭定时任务，无法重新启动")
    public void shutdown() throws SchedulerException {
        schedulerService.shutdown();
    }
}
